package com.me.mygdxgame.Components;

import java.util.ArrayList;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.me.mygdxgame.Component;
import com.me.mygdxgame.DIRECTION;
import com.me.mygdxgame.GameObjects.Tile;
import com.me.mygdxgame.GameObjects.Unit;

public class TilebasedCharacterController extends Component {
	
	static float speed = 20.0f;
	ArrayList<Vector2> path = new ArrayList<Vector2>();;
	Transform transform;
	
	public TilebasedCharacterController(Transform transform){
		this.transform = transform;
	}

	public ArrayList<Vector2> getPath(Tile startTile, Tile targetTile){
		
		Tile tileToCheck = startTile;
		ArrayList<Vector2> tempPath = new ArrayList<Vector2>();
		while(tileToCheck != targetTile){
			tempPath.add(getTilesPosition(tileToCheck));
			tileToCheck = getNeighbourNextToTarget(tileToCheck, getTilesPosition(targetTile));
		}
		tempPath.add(getTilesPosition(targetTile));
		return tempPath;
	}
	
	public Tile getNeighbourNextToTarget(Tile tileToCheck, Vector2 target){
		Tile topTile = tileToCheck.getNeighborTile(DIRECTION.UP);
		Tile bottomTile = tileToCheck.getNeighborTile(DIRECTION.DOWN);
		Tile rightTile = tileToCheck.getNeighborTile(DIRECTION.RIGHT);
		Tile leftTile = tileToCheck.getNeighborTile(DIRECTION.LEFT);
		
		ArrayList<Tile> validNeighbours = new ArrayList<Tile>();
		
		if(topTile != null){
			validNeighbours.add(topTile);
		}
		if(bottomTile != null){
			validNeighbours.add(bottomTile);
		}
		if(rightTile != null){
			validNeighbours.add(rightTile);
		}
		if(leftTile != null){
			validNeighbours.add(leftTile);
		}
		
		float closestDistance = 1000;
		Tile closestNeighbourTile = new Tile();
		for(Tile t:validNeighbours){
			if(getTilesPosition(t).dst(target) < closestDistance){
				closestDistance = getTilesPosition(t).dst(target);
				closestNeighbourTile = t;
			}
		}
		return closestNeighbourTile;
	}
	
	Vector2 getTilesPosition(Tile tile){
		Transform tilesTransform = (Transform)tile.getComponent("Transform");
		return tilesTransform.getPosition();
	}
	
	public boolean checkIfTargetReached(Vector2 target){
		if(transform.position.dst(target) > 0){
			return false;
		}
		return true;
	}
	
	void getNextPosition(){
		path.remove(0);
	}
	
	public void move(Tile startTile, Tile targetTile){
		
		Transform targetTileTransform = (Transform)targetTile.getComponent("Transform");
		Vector2 target = targetTileTransform.getPosition();
		if(!checkIfTargetReached(target)){
			if(path.size() > 0 ){
				Vector2 currentTarget = path.get(0);
				
				if(transform.position.dst(currentTarget) > 0){
					if(transform.position.dst(currentTarget) < 2f){
						transform.position = new Vector2(currentTarget.x, currentTarget.y);
					}
					if(this.transform.position.x - currentTarget.x > 0){
						transform.position.x -= Gdx.graphics.getDeltaTime() * speed;
					}
					else if(transform.position.x - currentTarget.x < 0){
						transform.position.x += Gdx.graphics.getDeltaTime() * speed;
					}
					else if(transform.position.y - currentTarget.y > 0){
						transform.position.y -= Gdx.graphics.getDeltaTime() * speed;
					}
					else if(transform.position.y - currentTarget.y < 0){
						transform.position.y += Gdx.graphics.getDeltaTime() * speed;
					}
				}
				else{
					getNextPosition();
				}
			}
			else{
				path.clear();
				path = getPath(startTile, targetTile);
			}
		}
	}
}
